﻿using System;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using System.Web;
using Microsoft.SharePoint.Utilities;

namespace CodeCounsel.SharePoint.Flash
{
    public class ViewVideoPage
        : UnsecuredLayoutsPageBase
    {
        protected FlashVideo FlashVideo;
        SPList _list;
        SPListItem _item;

        protected override bool AllowAnonymousAccess
        {
            get { return true; }
        }

        protected SPList List
        {
            get
            {
                if (_list == null)
                {
                    string listIDString = Request.QueryString["ListID"];
                    _list = SPContext.Current.Web.Lists[new Guid(listIDString)];
                }
                return _list;
            }
        }

        protected SPListItem Item
        {
            get
            {
                if (_item == null)
                {
                    string itemIDString = Request.QueryString["ItemID"];
                    _item = List.GetItemById(Int32.Parse(itemIDString));
                }
                return _item;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            SPListItem item = Item;
            if (item.DoesUserHavePermissions(SPBasePermissions.ViewListItems) == false)
            {
                SPUtility.Redirect(
                    SPUtility.AccessDeniedPage, SPRedirectFlags.Default, Context);
            }
            FlashVideo.VideoWidth = (int)item[FieldID.VideoWidth];
            FlashVideo.VideoHeight = (int)item[FieldID.VideoHeight];
            string previewImageUrl = (string)item[FieldID.VideoPreviewImage];
            if (String.IsNullOrEmpty(previewImageUrl))
            {
                previewImageUrl = Web.Properties[VideoSettings.BackgroundImage];
            }
            FlashVideo.PreviewImageUrl = previewImageUrl;
            FlashVideo.AutoStart = VideoSettings.GetWebProperty<bool>(VideoSettings.AutoStart, VideoSettings.AutoStartDefault);
            FlashVideo.ShowIcons = VideoSettings.GetWebProperty<bool>(VideoSettings.ShowIcons, VideoSettings.ShowIconsDefault);
            FlashVideo.FullScreenMode = VideoSettings.GetWebProperty<FullScreenMode>(VideoSettings.FullScreenMode, FullScreenMode.NoStretch);
            FlashVideo.VideoSource = SPContext.Current.Site.MakeFullUrl(
                item.File.ServerRelativeUrl);
            base.OnLoad(e);
        }
    }
}
